<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<title>权限管理</title>
<link rel="stylesheet" type="text/css" href="${ctx}/easyui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="${ctx}/easyui/easyui_my.css">
<link rel="stylesheet" type="text/css" href="${ctx}/easyui/themes/icon.css">
<link rel="stylesheet" type="text/css" href="${ctx}/css/css.css" />

<script type="text/javascript" src="${ctx}/js/jquery-1.7.min.js"></script>
<script type="text/javascript" src="${ctx}/easyui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="${ctx }/easyui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="${ctx}/js/common.js"></script>
<%-- <script type="text/javascript" src="${ctx}/js/easyUIOpenWindow.js"></script> --%>

<!--[if lte IE 6]>
	<script src="${ctx }/js/DD_belatedPNG_0.0.8a.js" type="text/javascript"></script>
	<script type="text/javascript">
	DD_belatedPNG.fix('.shequliuyang,.jjxinxi,ul, img, li, input , a, .png_bg');
	/* 将 .png_bg 改成你应用了透明PNG的CSS选择器*/
	</script>
	<![endif]-->
</head>
<body class="easyui-layout" options="border:false">
	
	<div data-options="region:'north',height:72,border:false" style="padding-left:10px;padding-right: 10px;padding-top: 5px;padding-bottom: 5px;">
		<div class="xinxichaxunkuangz1">
			<div class="dashujtiaojianxuanxiangkuan">
				<div class="dashujtiaojianxuanxiangkuan_lie">
						<div class="tiaojian_key">角色名称：</div>
						<div class="tiaojian_values">
							<ul class="tiaojian_values_limian">
								<li><input id="roleSelect" name="roleSelect" type="text" class="shurukuangkais" value="角色编号或名称" onblur="InputBlur('roleSelect','角色编号或名称')" onfocus="InputFocus('roleSelect','角色编号或名称')" /></li>
							</ul>
						</div>
				</div>
				<div class="chaxunchongzhianniu">
					<a href="javascript:AddWindow()" class="sousuocx">添加</a>
					<a href="javascript:deleteRoles()" class="congzhim">删除</a>
					<a href="javascript:queryDataGrid()" class="congzhim">查询</a>
				</div>
			</div>
		</div>
	</div>
	<div data-options="region:'center',border:false" style="padding-left:10px;padding-right: 10px;padding-top: 0px;">
		<div class="easyui-layout xinxichaxunkuangz2" data-options="fit:true,border:false">
			<div class="tupianqiehuantop" data-options="region:'north',border:false" style="height: 40px">
              	<span class="chaxunjieguosd">搜索结果</span>
     		</div>
     		<div data-options="region:'center',border:false">
     			<table id="TableData" data-options="fit:true"></table>
     		</div>
     		<jsp:include page="/WEB-INF/content/role/addOrUpdateRole.jsp" />
		</div>
	</div>
	<script type="text/javascript">
	
		$(function(){
			initTable();
			//初始化Ajax表单提交
			$("#saveOrUpdateDevForm").form({
				url:'${ctx }/role/saveRole.htm',
				onSubmit:function(param){//提交前校验
					var nodes = getCheckedExt();
					var ids="";
					for(var i=0;i<nodes.length;i++){
						ids+="|"+nodes[i].id;
					};
					param.ids=ids;
					return true;
				},
				success:function(data){//ajax提交后获取数据库返回信息
					if(data == 'true'){
						cancelUpdateDev();
						$("#TableData").datagrid("load");
					}else{
						$.messager.show({
							title:'操作失败',
							msg:'操作失败，请联系管理人员',
							timeout:5000,
							showType:'slide'
						});
					}
				}
			});
			
			
		});
		function initTable(){
			$("#TableData").datagrid({
				url:'${ctx}/role/findRoleByPage.htm',
				idField:'rid',
				fitColumns:true,
				border:false,
				striped:true,
				//singleSelect:true,
				pagination: true,
				pageSize: 10,                //每页记录数      
	           	pageList: [5, 10, 15, 20, 30, 50], //分页记录数数组 
// 				queryParams:{
// 					pageSize:5
// 					,page:1
// 				},
				columns:[[
					{field:'',checkbox:true,align:'center',split:true},
					{field:'rid',title:'角色编号',width:'130',align:'center',split:true,fixed:true},
				  	{field:'rname',title:'角色名称',width:'130',align:'center',split:true,fixed:true},
				  	{field:'ipks',title:'起始登录IP',width:'150',align:'center',split:true,fixed:true},
				  	{field:'ipjs',title:'结束登录IP',width:'150',align:'center',split:true,fixed:true},
				  	{field:'cjsjStr',title:'角色创建时间',width:'150',align:'center',split:true,fixed:true},
				  	{field:'cjrxm',title:'创建人',width:'130',align:'center',split:true,fixed:true},
				  	{field:'jsyxqStr',title:'角色有效期',width:'150',align:'center',split:true,fixed:true},
				  	{field:'cz',title:'操作',width:'100',align:'center',split:true,fixed:true,formatter:function(value,row,index){
				  		return "<a class=\"chense\" href=\"javascript:UpdateRoleInfo('"+row.rid+"')\" >修改</a>&nbsp;&nbsp;&nbsp;<a class=\"chense\" href=\"javascript:deleteRole('"+row.rid+"')\" >删除</a>";
				  	}}
				]]
			});
		}
		
		//根据界面中的样件，过滤查询用户列表
		function queryDataGrid(){
			var roleSelect = $("#roleSelect").val() == "角色编号或名称" ? "":$("#roleSelect").val();
			$("#TableData").datagrid("load",{
				rid:roleSelect
			});
		}
// 		function queryDataGrid(){
// 			var roleSelect = $("#roleSelect").val() == "角色编号或名称" ? "":$("#roleSelect").val();
// 			$("#TableData").datagrid("load",{
// 				yhdh:yhSelect,
// 				rid:ridSelect
// 			});
// 		}
		
		//弹出用户对话框
		function UpdateRoleInfo(rid){
			findRoleMenuTree(rid);
			$('#saveOrUpdateDevForm').form('clear');
			$("#ridtr").css('display','');
			$("#saveLx").attr("value","xg");
			$('#SaveOrUpdateDevWin').window('open');
			$.ajax({
				type:'POST',
				url:'${ctx}/role/findRoleByRid.htm',
				data:{rid:rid},
				dataType:'json',
				success:function(data, textStatus, jqXHR){
					$('#saveOrUpdateDevForm').form('load',data);
				}
			});
			//$('#saveOrUpdateDevForm').form('load','${ctx}/user/findUserByYhdh2.htm?yhdh='+yhdh+"&timesampe="+new Date().getTime());
		}
		
		//弹出新增用户对话框
		function AddWindow(){
			$('#saveOrUpdateDevForm').form('clear');
			$("#rid").attr("value","");
			$("#ridtr").css('display','none');
			$("#saveLx").attr("value","");
			$('#SaveOrUpdateDevWin').window('open');
		}
		
		//保存用户信息
		function updateDev(){
			var f=0;
			var a = new Array();
			a.push("rname|角色名称");
			for(var i=0;i<a.length;i++){
				var id=a[i].split("|")[0];
				var name=a[i].split("|")[1];				
				if($("#"+id).val()==""){
					f=1;
					alert(name+"不能为空！");
					return;
				}
			}
			if($("#ipks").val()!=""){
				if(!ipIsValid($("#ipks").val())){
					alert("开始的IP格式不正确！");
					f=1;
					return;
				}
			}
			if($("#ipjs").val()!=""){
				if(!ipIsValid($("#ipjs").val())){
					alert("结束的IP格式不正确！");
					f=1;
					return;
				}
			}
			if(!lengthIsValid($("#rname").val())){
				alert("角色名称不能超过32个字符！");
				f=1;
				return;
			}
			if(f==0){
				$("#saveOrUpdateDevForm").submit();
			}
		}

		function getCheckedExt(){//获取选中节点包括实心
			var checked = $("#menuTree").tree('getChecked');
			var checkbox2 = $("#menuTree").tree('getChecked', 'indeterminate');
			$.each(checkbox2,function(i,n){
			    checked.push(n); 
			}); 
			return checked; 
		}
		
		//取消保存用户
		function cancelUpdateDev(){
			$("#saveOrUpdateDevForm")[0].reset();
			$("#SaveOrUpdateDevWin").window("close");
		}
		
		//删除用户信息
		function deleteRole(ids){
			if(ids=="1001"){
				alert("超级管理员不允许删除！");
				return;
			}else{
				var r=confirm("确认要删除角色信息吗？删除后将不可恢复！")
				if(r){
					$.ajax({
						url:'${ctx}/role/delRole.htm',
						type:'POST',
						data:{
							ids:ids
						},
						success:function(data){
							if(data =="true"){
								queryDataGrid();
							}else{
								$.messager.show({
									title:'操作失败',
									msg:'操作失败，请联系管理人员',
									timeout:5000,
									showType:'slide'
								});
							}
						}
					});
				}
			}
		}
		//删除用户信息
		function deleteRoles(){
			var ids = $("#TableData").datagrid("getSelections");//getSelections
			var selectValues = "";
			var f=0;//用于判断删除数据中是否有admin
			for(var i=0;i<ids.length;i++){
				if(ids[i].rid=='1001'){
					f=1;
					alert("超级管理员不允许删除！");
					break;
				}else{
					if(i==0){
						selectValues+=ids[i].rid;
					}else{
						selectValues+=","+ids[i].rid;
					}
				}
			}
			if(f==1){
				return;
			}
			if(selectValues ==""){
				alert("请先选择要删除的记录！");
				return;
			}
			var r=confirm("确认要删除用户信息吗？删除后将不可恢复！")
			if(r){
				$.ajax({
					url:'${ctx}/role/delRole.htm',
					type:'POST',
					data:{
						ids:selectValues
					},
					success:function(data){
						if(data =="true"){
							queryDataGrid();
						}else{
							$.messager.show({
								title:'操作失败',
								msg:'操作失败，请联系管理人员',
								timeout:5000,
								showType:'slide'
							});
						}
					}
				});
			}
		}
		function ipIsValid(ip){
			var regex = new RegExp("\^((25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\\.){3}(25[0-5]|2[0-4]\\d|1\\d{2}|[1-9]\\d|\\d)\$");   
			return regex.test(ip);
		}
		function lengthIsValid(str){
			var strlength= str.replace(/[^x00-xff]/g,"01").length;
			return strlength<=32;
		}
	</script>
</body>
</html>